// pages/signIn/index.js
const app = getApp();
const API = require('../../common/constant');
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    continueData: 0,
    totalData: 0,
    isDraw: false,
    state: '今日尚未签到',
    month: '2018年7月',
    btnText: '签到',
    days_style: '',
    isSign: '',
    isClick: false,
    awardCount: ''
  },
  scroll() {
    wx.pageScrollTo({
      scrollTop: 500
    })
    let days_style = this.data.days_style
    this.setData({
      days_style: days_style
    })
  },
  enterPage(){
    util.httpbody({
      url: API.URL.SIGN.FIND_SING_LOG,
      success: res => {
        console.log(res)
        let bussData = res.data.bussData
        let signDate = bussData.signDate
        this.setData({
          continueData: bussData.continueSignCount,
          totalData: bussData.cumulativeCount,
          isSign: bussData.isSign === 'y' ? true: false,
          btnText: bussData.isSign === 'y' ? '已签到' : '签到',
          state: bussData.isSign === 'y' ? '已签到' : '今日尚未签到'
        })
        let signArr = [];
        for(let i in signDate){
          signArr[i] = parseInt(signDate[i].split('-')[2].split(' '));
        }
        let days_style = new Array;
        for (let i = 1; i <= 31; i++) {
          days_style.push({
            month: 'current', day: i, color: '#333333', background: '#fff',
          });
        }
        for (let q in signArr) {
          days_style.push(
            { month: 'current', day: signArr[q], color: 'white', background: 'linear-gradient(to right, #FF8023, #FE3232)' }
          );
        }
        this.setData({
          days_style: days_style
        })
        this.getAwardNum()
      },
      fail: err => {
        console.log('ent err', err)
      }
    })
  },
  getAwardNum() {
    util.http({
      url: API.URL.AWARD.FIND_AWARD,
      success: res => {
        console.log('res', res)
        let bussData = res.data.bussData
        if (bussData.awardCount > 0) {
          this.setData({
            isDraw: true,
            awardCount: bussData.awardCount
          })
          if (this.data.isSign) {
            this.setData({
              btnText: '立即抽奖'
            })
          }
        }
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },
  saveSign() {
    if(this.data.isSign || this.data.isClick){
      if (this.data.isDraw) {
        wx.redirectTo({
          url: '../draw/index',
        })
      }
      return
    }
    this.setData({
      isClick: true
    })
    wx.showLoading()
    util.http({
      url: API.URL.SIGN.SAVE_SIGN_LOG,
      success: (res) => {
        console.log('签到成功', res)
        this.setData({
          isClick: false
        })
        this.enterPage()
        wx.hideLoading()
        app.showUserInfo('签到成功', 'none', 1500)
      },
      fail: (err) => {
        console.log('签到失败', err)
        this.setData({
          isClick: false
        })
        wx.hideLoading()
        app.showUserInfo('签到失败', 'none', 1500)        
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.enterPage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})